********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************
global y "lncdisbur_w199"
global regcommands ", pool(2) compact absorb(bankid borrowerid#time) vce(cluster bankid)"
global regcommands2 ", pool(2) compact absorb(bankid#year borrowerid#time) vce(cluster bankid)"

clear *
**# Bookmark (input)
import sas using $inputdirectory/bloansmonpol, case(lower)
keep if disbur>=30000|com>=30000
drop com

format borrowerid %20.0g
format disbur %20.0g

tostring(time), replace
gen xxx=date(time, "YMD")
drop time
gen time=mofd(xxx)
drop xxx
format time %tm
gen xxx=dofm(time)
gen year=year(xxx)
drop xxx

**# Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_typesmonpol, keepus(type)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_grumonpol, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.

gen xxx=type if bankid==gru
egen grutype=max(xxx), by(gru)
replace grutype=10 if grutype==.
drop xxx
keep if grutype==10

drop bankid
rename gru bankid

collapse (sum) disbur (max) year, by(bankid borrowerid time)

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2010m7)&time<=tm(2020m2)
drop if time==tm(2018m1)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)
egen nrel=count(bankid), by(borrowerid time)
keep if nrel>=2
drop nrel lncdisbur

**# Bookmark (input)
merge m:1 time using $inputdirectory/INB_shocksmonpol
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_contr&expmonpol
keep if _merge!=2
drop _merge

gen count=1
sort bankid time
bys bankid time: gen uniquebankidtime=sum(count)

_pctile lagmma if uniquebankidtime==1, p(66 85)
gen dtoplagmma=(lagmma>r(r1))
replace dtoplagmma=(lagmma>r(r2)) if time<tm(2018m2)

gen IT10Ydtoplagmma=IT10Y*dtoplagmma
gen IT10Ylagmma=IT10Y*lagmma

**# Bookmark (input)
merge m:1 bankid year using $inputdirectory/INB_capitalmonpol
keep if _merge==3

sum ourt1r if uniquebankid==1, d
scalar define s1=r(p25)
gen ourt1rold=ourt1r
replace ourt1r=ourt1r-scalar(s1)
gen IT10Ylagmmaourt1r=IT10Ylagmma*ourt1r
gen IT10Yourt1r=IT10Y*ourt1r
gen lagmmaourt1r=lagmma*ourt1r
gen IT10Ylagcashas=IT10Y*lagcashas
gen lagmmalagcashas=lagmma*lagcashas
gen IT10Ylagmmalagcashas=IT10Ylagmma*lagcashas
gen IT10Ylagmmaourt1rlagcashas=IT10Ylagmmaourt1r*lagcashas
gen IT10Yourt1rlagcashas=IT10Yourt1r*lagcashas
gen lagmmaourt1rlagcashas=lagmmaourt1r*lagcashas
gen ourt1rlagcashas=ourt1r*lagcashas
drop count uniquebankid uniquebankidtime
compress

reghdfe $y IT10Ylagmma lagmma IT10Ylagmmaourt1r IT10Yourt1r lagmmaourt1r ourt1r IT10Ylagmmalagcashas IT10Ylagcashas lagmmalagcashas lagcashas IT10Ylagmmaourt1rlagcashas IT10Yourt1rlagcashas lagmmaourt1rlagcashas ourt1rlagcashas laglnassets lncrefin $regcommands
keep if e(sample)==1

**# Bookmark (output - column 1-2-3)
cd $outputdirectory
est clear
reghdfe $y IT10Ydtoplagmma dtoplagmma laglnassets lagcashas lncrefin $regcommands
est save TABVIIIA_C123
reghdfe $y IT10Ylagmma lagmma laglnassets lagcashas lncrefin if time<tm(2016m10)|time>tm(2017m12) $regcommands
est save TABVIIIA_C123, append
reghdfe $y IT10Ylagmma lagmma laglnassets lagcashas lncrefin $regcommands2
est save TABVIIIA_C123, append
cd ..

